package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址簿管理
 */
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrap=new LambdaQueryWrapper<>();
        queryWrap.eq(null!=addressBook.getUserId(),AddressBook::getUserId,addressBook.getUserId());
        queryWrap.orderByDesc(AddressBook ::getUpdateTime);

        //SQL:select * from address_book where user_id=? order by update_time desc
        return R.success(addressBookService.list(queryWrap));
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook ::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook ::getIsDefault,0);
//        SQL:update address_book set is_default=0 where uder_id=?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
//        SQL:update address_book set id_default=1 where id=?
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        AddressBook address = addressBookService.getById(id);
        if(address!=null){
            return R.success(address);
        }
        else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook ::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook ::getIsDefault,1);

        //SQL:select * from address_book where user_id = ? and id_default=1
        AddressBook address = addressBookService.getOne(queryWrapper);

        if(null!=address){
            return R.success(address);
        }
        else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 更新地址簿
     * @param address
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook address) {
        address.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",address);
        addressBookService.updateById(address);

        return R.success("修改成功");
    }
}
